package org.eng2.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

public class ClientePremiadoDAO {
	
	protected ArrayList<ClientePremiado> getAll(){
		PreparedStatement ps;
		ClientePremiado cp = new ClientePremiado();
		
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t.cod, m.id num_mesa, c.total ");
		sql.append("FROM token t ");
		sql.append("inner join mesa m on m.id = t.mesa_id ");
		sql.append("inner join conta c on t.cod = c.token_cod ");
		sql.append("where 1=1 ");
		sql.append("and m.ativa = 1 ");
		sql.append("and c.total >= 200.00 ");
		
		ArrayList<ClientePremiado> cpList = new ArrayList<ClientePremiado>();
		ResultSet rs;
		try {
			ps = DataBase.getConnectionDB().prepareStatement(sql.toString());
			rs = ps.executeQuery();
			while(rs.next()){
				cp.setCod_token(rs.getString("cod"));
				cp.setNum_mesa(rs.getInt("num_mesa"));
				cp.setTotal(rs.getInt("total"));
				cpList.add(cp);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		return cpList;
	}
	
	protected ClientePremiado getClientePremiado(String cod){
		PreparedStatement ps;
		ClientePremiado cp = new ClientePremiado();
		
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t.cod, m.id num_mesa, c.total ");
		sql.append("FROM token t ");
		sql.append("inner join mesa m on m.id = t.mesa_id ");
		sql.append("inner join conta c on t.cod = c.token_cod ");
		sql.append("where 1=1 ");
		sql.append("and m.ativa = 1 ");
		sql.append("and c.total >= 200.00 ");
		sql.append("and t.cod = ? ");
		
		ResultSet rs;
		try {
			ps = DataBase.getConnectionDB().prepareStatement(sql.toString());
			ps.setString(1, cod);
			rs = ps.executeQuery();
			if (rs.next()) {
				cp.setCod_token(rs.getString("cod"));
				cp.setNum_mesa(rs.getInt("num_mesa"));
				cp.setTotal(rs.getInt("total"));
			}
			return cp;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
}
